package com.yeebo.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.yeebo.common.BaseController;
import com.yeebo.interceptor.Access;
import com.yeebo.rpc.admin.SysRoleService;
import com.yeebo.utils.ResponseUtils;
import com.yeebo.vo.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("sysRole/manage")
public class SysRoleManageController extends BaseController {

    @Resource
    private SysRoleService sysRoleService;

    @Access
    @PostMapping(value = "/sysRoleList")
    @RequiresPermissions("MENU_2019109102812801")
    @ResponseBody
    public Result sysRoleList(@RequestBody JSONObject requestBody,
                              HttpServletRequest request) {
        Integer pageNo = requestBody.getInteger("pageNo");
        Integer pageSize = requestBody.getInteger("pageSize");
        String filterRoleName = requestBody.getString("filterRoleName");
        return ResponseUtils.success(sysRoleService.loadSysRoleManageList(pageNo, pageSize, filterRoleName));
    }

    @Access
    @PostMapping(value = "/addSysRole")
    @RequiresPermissions("RESOURCE_2019109103454974")
    @ResponseBody
    public Result addSysRole(@RequestBody JSONObject requestBody,
                             HttpServletRequest request) {
        String roleCode = requestBody.getString("roleCode");
        String roleName = requestBody.getString("roleName");
        String roleDesc = requestBody.getString("roleDesc");
        sysRoleService.addSysRole(roleCode, roleName, roleDesc);
        return ResponseUtils.success("添加成功");
    }

    @Access
    @PostMapping(value = "/editSysRole")
    @RequiresPermissions("RESOURCE_2019109103529643")
    @ResponseBody
    public Result editSysRole(@RequestBody JSONObject requestBody,
                              HttpServletRequest request) {
        Long roleId = requestBody.getLongValue("roleId");
        String roleName = requestBody.getString("roleName");
        String roleDesc = requestBody.getString("roleDesc");
        sysRoleService.updateSysRole(roleId, roleName, roleDesc);
        return ResponseUtils.success("更新成功");
    }

    @Access
    @PostMapping(value = "/deleteSysRole")
    @RequiresPermissions("RESOURCE_2019109103529643")
    @ResponseBody
    public Result deleteSysRole(@RequestBody JSONObject requestBody,
                                HttpServletRequest request) {
        Long roleId = requestBody.getLongValue("roleId");
        sysRoleService.deleteSysRole(roleId);
        return ResponseUtils.success("删除成功");
    }

    @Access
    @PostMapping(value = "/getSysRoleById")
    @ResponseBody
    public Result getSysRoleById(@RequestBody JSONObject requestBody,
                                 HttpServletRequest request) {
        Long roleId = requestBody.getLongValue("roleId");
        return ResponseUtils.success(sysRoleService.getSysRoleById(roleId));
    }

    @Access
    @PostMapping(value = "/loadSysResourcesVOByRoleId")
    @ResponseBody
    public Result loadSysResourcesVOByRoleId(@RequestBody JSONObject requestBody,
                                             HttpServletRequest request) {
        Long roleId = requestBody.getLongValue("roleId");
        return ResponseUtils.success(sysRoleService.loadSysResourcesVOByRoleId(roleId));
    }

    @Access
    @PostMapping(value = "/assignResourcesToRole")
    @RequiresPermissions("RESOURCE_2019109103546901")
    @ResponseBody
    public Result assignResourcesToRole(@RequestBody JSONObject requestBody,
                                        HttpServletRequest request) {
        Long roleId = requestBody.getLongValue("roleId");
        String[] resourceIds = requestBody.getString("resourceIdList").split(",");
        List<Long> targetResourceIds = new LinkedList<>();
        for (String resourceIdStr : resourceIds) {
            targetResourceIds.add(Long.parseLong(resourceIdStr));
        }
        sysRoleService.assignResourcesToRole(roleId, targetResourceIds);
        return ResponseUtils.success("成功分配对应的资源权限");
    }

}
